<?php

if (isset($_POST['username']))
    $username = " value=\"" . $_POST['username'] . "\"";
if (isset($_POST['firstName']))
    $firstName = " value=\"" . $_POST['firstName'] . "\"";
if (isset($_POST['lastName']))
    $lastName = " value=\"" . $_POST['lastName'] . "\"";
if (isset($_POST['username2']))
    $username2 = " value=\"" . $_POST['username2'] . "\"";
if (isset($_POST['email']))
    $email = " value=\"" . $_POST['email'] . "\"";
if (isset($_POST['birthMonth']))
    $birthMonth = " value=\"" . $_POST['birthMonth'] . "\"";
if (isset($_POST['birthDay']))
    $birthDay = " value=\"" . $_POST['birthDay'] . "\"";
if (isset($_POST['birthYear']))
    $birthYear = " value=\"" . $_POST['birthYear'] . "\"";
if (isset($_POST['phone']))
    $phone = " value=\"" . $_POST['phone'] . "\"";
if (isset($_POST['street']))
    $street = " value=\"" . $_POST['street'] . "\"";
if (isset($_POST['zip']))
    $zip = " value=\"" . $_POST['zip'] . "\"";
if (isset($_POST['state']))
    $state = " value=\"" . $_POST['state'] . "\"";
if (isset($_POST['school']))
    $school = " value=\"" . $_POST['school'] . "\"";

$login = $_GET['login'];
$register = $_GET['register'];

$loginForm = "<form action=\"index.php?login=1&submit=1\" method=\"POST\">
        <table><tr><td>Username</td><td><input type=\"text\" name=\"username\" $username></td></tr>
        <tr><td>Password</td><td><input type=\"password\" name=\"password\"></td></tr>
        </table><br /><center><input type=\"submit\" value=\"Login\"></form></center>";

$twitterLogin = "<a href=\"http://pluto.cse.msstate.edu/~dcspg8/dcsp-portal-project/twitter_login.php\" target=\"_blank\"> <img src=\"images\lighter.png\"> </a>";

$registerForm = "<form method=\"POST\" action=\"index.php?register=1&submit=1\">
    <table>
    <tr><td>*First Name:</td><td><input type=\"text\" name=\"firstName\" $firstName></td></tr>
    <tr><td>Last Name:</td><td><input type=\"text\" name=\"lastName\" $lastName></td></tr>
    <tr><td>*Username:</td><td><input type=\"text\" name=\"username2\"  $username2></td></tr>
    <tr><td>*Password:</td><td><input type=\"password\" name=\"password2\" /></td></tr>
    <tr><td>*Re-enter Password:</td><td><input type=\"password\" name=\"password3\" /></td></tr>
    <tr><td>*Email:</td><td><input type=\"text\" name=\"email\" $email></td></tr>
    <tr><td>*Birth Date (MM/DD/YYYY):</td><td>

<select name=\"birthMonth\">
                        <option value=\"01\">January</option>
                        <option value=\"02\">February</option>
                        <option value=\"03\">March</option>
                        <option value=\"04\">April</option>
                        <option value=\"05\">May</option>
                        <option value=\"06\">June</option>
                        <option value=\"07\">July</option>
                        <option value=\"08\">August</option>
                        <option value=\"09\">September</option>
                        <option value=\"10\">October</option>
                        <option value=\"11\">November</option>
                        <option value=\"12\">December</option>
                    </select>
                    <select name=\"birthDay\">
                        <option value=\"01\">01</option>
                        <option value=\"02\">02</option>
                        <option value=\"03\">03</option>
                        <option value=\"04\">04</option>
                        <option value=\"05\">05</option>
                        <option value=\"06\">06</option>
                        <option value=\"07\">07</option>
                        <option value=\"08\">08</option>
                        <option value=\"09\">09</option>
                        <option value=\"10\">10</option>
                        <option value=\"11\">11</option>
                        <option value=\"12\">12</option>
                        <option value=\"13\">13</option>
                        <option value=\"14\">14</option>
                        <option value=\"15\">15</option>
                        <option value=\"16\">16</option>
                        <option value=\"17\">17</option>
                        <option value=\"18\">18</option>
                        <option value=\"19\">19</option>
                        <option value=\"20\">20</option>
                        <option value=\"21\">21</option>
                        <option value=\"22\">22</option>
                        <option value=\"23\">23</option>
                        <option value=\"24\">24</option>
                        <option value=\"25\">25</option>
                        <option value=\"26\">26</option>
                        <option value=\"27\">27</option>
                        <option value=\"28\">28</option>
                        <option value=\"29\">29</option>
                        <option value=\"30\">30</option>
                        <option value=\"31\">31</option>
                    </select>

<input type=\"text\" name=\"birthYear\" size=4 $birthYear />
</td></tr>
    <tr><td>Sex:</td><td>Gal <input type=\"radio\" name=\"sex\" value=\"F\"  /> Guy <input type=\"radio\" name=\"sex\" value=\"M\" /></td></tr>
    <tr><td>Phone Number:</td><td><input type=\"text\" name=\"phone\" $phone/></td></tr>
    <tr><td>Street:</td><td><input type=\"text\" name=\"street\" $street/></td></tr>
    <tr><td>Zip Code:</td><td><input type=\"text\" name=\"zip\" $zip></td></tr>
    <tr><td>State:</td><td><input type=\"text\" name=\"state\" $state></td></tr>
    <tr><td>School:</td><td><input type=\"text\" name=\"school\" $school></td></tr>
    <tr><td><center><input type=\"reset\" value=\"Reset Values\" /></center></td><td><center><input name=\"register\" type=\"submit\" value=\"Register\"></center></td></tr>
    </table></form>
    ";

$form = "<table><tr><td valign=\"top\">$twitterLogin<br>$loginForm</td><td>$registerForm</td></tr></table>
    ";

require_once('userManager.php');

if ($login == "1") {
    $registerForm = "";
    if ($submit == "1") {
        $encrypted = crypt(md5($_POST['password']), md5($_POST['username']));
        echo $_POST['username'] . "<br>" . $_POST['password'] . "<br>" . $encrypted;
        $loginBool = userManager::login($_POST['username'], $_POST['password']);
        if ($loginBool == 0)
            echo $form;
    } else {
        echo $form;
    }
} else if ($register == "1") {
    $loginForm = "";
    if ($submit == "1") {
        $errorMessage = "";
        if (!isset($_POST['firstName']))
            $errorMessage = "You failed to enter your first name.";
        else if (!isset($_POST['username2']))
            $errorMessage = "You failed to enter a username.";
        else if (!isset($_POST['password2']))
            $errorMessage = "You failed to enter a password.";
        else if (!isset($_POST['password3']))
            $errorMessage = "You failed to re-enter a password.";
        else if ($_POST['password3'] != $_POST['password2'])
            $errorMessage = "Your passwords did not match.";
        else if (!isset($_POST['email']))
            $errorMessage = "You failed to enter an email.";
        else if (!isset($_POST['birthMonth']) || !isset($_POST['birthDay']) || !isset($_POST['birthYear']))
            $errorMessage = "You did not enter a proper date.";
        else if (isset($_SESSION['userID']))
            $errorMessage = "You are already logged in.";
        else if (strlen($_POST['birthMonth']) != 2 || strlen($_POST['birthDay']) != 2 || strlen($_POST['birthYear']) != 4)
            $errorMessage = "You have entered your birthday in the wrong format.";

        if ($errorMessage == "") {
            $encrypted = crypt(md5($_POST['password2']), md5($_POST['username2']));
            echo $_POST['username2'] . "<br>" . $_POST['password2'] . "<br>" . $encrypted;
            $birthdate = $_POST['birthYear'] . "-" . $_POST['birthMonth'] . "-" . $_POST['birthDay'];
            $address = $_POST[street] + "\n" + $_POST[city] + ", " + $_POST[state] + " " + $_POST[zip];

            $newUser = new User();
            $newUser->setFName($_POST[firstName]);
            $newUser->setLName($_POST[lastName]);
            $newUser->setUsername($_POST[username2]);
            $newUser->setPassword($encrypted);
            $newUser->setEmail($_POST[email]);
            $newUser->setPhone($_POST[phone]);
            $newUser->setAddress($address);
            $newUser->setBirthday($birthdate);
            $registerBool = userManager::registerUser($newUser);
            if ($registerBool == 0)
                echo $form;
        } else
            echo $errorMessage . $form;
    } else {
        echo $form;
    }
} else {
    echo $form;
}
?>